package com.haoshuang.zhouzhoubang.wxapi;

/**
 * Copyright (c)
 *
 * @Description
 * @Author 你真好看
 * @Date 2018年11月22日 16:39:22
 * @CreateBy Android Studio
 */

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.WindowManager;
import android.widget.Toast;

import com.haoshuang.zhouzhoubang.MyApp;
import com.haoshuang.zhouzhoubang.config.Config;
import com.haoshuang.zhouzhoubang.model.event.WeChatLoginEvent;
import com.haoshuang.zhouzhoubang.utils.PreferenceUtils;
import com.jeremyliao.liveeventbus.LiveEventBus;
import com.tencent.mm.opensdk.modelbase.BaseReq;
import com.tencent.mm.opensdk.modelbase.BaseResp;
import com.tencent.mm.opensdk.modelmsg.SendAuth;
import com.tencent.mm.opensdk.modelmsg.SendMessageToWX;
import com.tencent.mm.opensdk.openapi.IWXAPI;
import com.tencent.mm.opensdk.openapi.IWXAPIEventHandler;
import com.tencent.mm.opensdk.openapi.WXAPIFactory;

/**
 * 微信客户端回调activity示例
 */
public class WXEntryActivity extends Activity implements IWXAPIEventHandler {

    private IWXAPI api;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        // 隐藏状态栏
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
                WindowManager.LayoutParams.FLAG_FULLSCREEN);
        //接收到分享以及登录的intent传递handleIntent方法，处理结果
        api = WXAPIFactory.createWXAPI(this, Config.WX_ID, false);
        api.handleIntent(getIntent(), this);
    }
    @Override
    public void onReq(BaseReq baseReq) {

    }

    @Override
    public void onResp(BaseResp baseResp) {
       // finish();
        //登录回调
        switch (baseResp.errCode) {
            case BaseResp.ErrCode.ERR_OK:
                if (baseResp instanceof SendAuth.Resp){
                    String code = ((SendAuth.Resp) baseResp).code;
                   // Toast.makeText(App.Companion.getContext(),"用户同意授权", Toast.LENGTH_SHORT).show();
                   String type= PreferenceUtils.getString("wx_type");

                    if (type.equals("1")){
                        Log.e("onResp","zhixxxx");
                        LiveEventBus.get("Wxs").post(code);

                       // EventBus.getDefault().post(new Event(99220,code));
                    }else if (type.equals("2")){
                        // 账号密码微信登录
                        LiveEventBus.get(WeChatLoginEvent.class).post(new WeChatLoginEvent(code));

                       // EventBus.getDefault().post(new Event(99221,code));
                    }

                }else {
                    SendMessageToWX.Resp code = ((SendMessageToWX.Resp) baseResp);
                    Toast.makeText(MyApp.Companion.getInstance(),"分享成功", Toast.LENGTH_SHORT).show();
                }
                finish();
                break;
            case BaseResp.ErrCode.ERR_AUTH_DENIED://用户拒绝授权
                Toast.makeText(MyApp.Companion.getInstance(),"用户拒绝授权", Toast.LENGTH_SHORT).show();
                finish();
                break;
            case BaseResp.ErrCode.ERR_USER_CANCEL://用户取消
                if (baseResp instanceof SendAuth.Resp){

                }else {
                    Log.e("WXEntryActivity","取消分享");
                }
                Toast.makeText(MyApp.Companion.getInstance(),"用户取消", Toast.LENGTH_SHORT).show();
                finish();
                break;
            default:
                finish();
                break;
        }


    }



    @Override
    public void onPointerCaptureChanged(boolean hasCapture) {

    }
}
